<template>
  <div v-loading="vloading">
    <el-row style="margin-top: 15px">
      <el-col :span="12" :offset="0">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="原密码" prop="oldpass">
            <el-input
              type="password"
              v-model="ruleForm.oldpass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="pass">
            <el-input
              type="password"
              v-model="ruleForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input
              type="password"
              v-model="ruleForm.checkPass"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import key from "@/components/js/key.js";
import { update, login } from "@/api/user";
export default {
  data() {
    var checkoldpass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入原密码"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      vloading: false,
      ruleForm: {
        oldpass: "",
        pass: "",
        checkPass: "",
      },
      rules: {
        oldpass: [{ validator: checkoldpass, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.vloading = true;
      let that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(valid);
          console.log(that.ruleForm.oldpass, that.$getuserinfo().password);
          if (that.ruleForm.oldpass == that.$getuserinfo().password) {
            update({
              field: "id",
              value: that.$getuserinfo().id,
              password: that.ruleForm.pass,
            }).then((res) => {
              login({
                tel: that.$getuserinfo().tel,
                password: that.ruleForm.pass,
              }).then((res) => {
                this.vloading = false;
                console.log(res);
                const user = key.jiami(JSON.stringify(res));
                sessionStorage.setItem("admin", JSON.stringify(res));
                sessionStorage.setItem("adminkey", JSON.stringify(user));
                that.$message({
                  message: "修改成功！",
                  type: "success",
                });
              });
            });
          } else {
            that.$message({
              message: "密码错误！",
              type: "error",
            });
            this.vloading = false;
          }
        } else {
          this.vloading = false;
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
</style>